package cn.javaxc.mvcwork.modules.site;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.framework.database.*;
import cn.javaxc.mvcwork.modules.site.*;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.Connection;
import java.util.Map;




public class SiteUtils {
	  
  
  public static SiteList getSiteList(MVCConfig prefs, ConnectionPool cp) {
    return SiteUtils.getSiteList(prefs.getPrefs(), cp);
  }

  public static SiteList getSiteList(MVCConfig prefs, ConnectionPool cp, String vhost) {
    return SiteUtils.getSiteList(prefs.getPrefs(), cp, vhost);
  }

  
  public static SiteList getSiteList(Map config) {
    return getSiteList(config, null);
  }

  public static SiteList getSiteList(Map config, ConnectionPool cp) {
    return getSiteList(config, cp, (String) null);
  }

  public static SiteList getSiteList(Map config, ConnectionPool cp, String vhost) {
    Connection dbSites = null;
    SiteList siteList = new SiteList();
    String appCode = (String) config.get(MVCConstant.DB_CODE);
    String baseName = (String) config.get("gatekeeper.url");
    String dbUser = (String) config.get("gatekeeper.user");
    String dbPass = (String) config.get("gatekeeper.password");
    String driver = (String) config.get("gatekeeper.driver");
    try {
      if (MVCConfig.getConfig().isSiteMode()) {
        if (cp != null) {
          ConnectionElement ce = new ConnectionElement(
              baseName, dbUser, dbPass);
          ce.setDriver(driver);
          dbSites = cp.getConnection(ce);
        } else {
          //Build list of sites to process
          Class.forName(driver);
          dbSites = DatabaseUtils.getConnection(baseName, dbUser, dbPass);
        }
        siteList.setSiteCode(appCode);
        siteList.setEnabled(MVCConstant.TRUE);
        siteList.setVirtualHost(vhost);
        siteList.buildList(dbSites);
        if (cp == null) {
          dbSites.close();
        }
      } else {
        //This setup only allows one site so process it
        Site thisSite = new Site();
        thisSite.setDatabaseDriver((String) config.get("gatekeeper.driver"));
        thisSite.setDatabaseUrl((String) config.get("gatekeeper.url"));
        thisSite.setDatabaseName((String) config.get("gatekeeper.database"));
        thisSite.setDatabaseUsername((String) config.get("gatekeeper.user"));
        thisSite.setDatabasePassword(
            (String) config.get("gatekeeper.password"));
        thisSite.setSiteCode((String) config.get(MVCConstant.DB_CODE));
        thisSite.setVirtualHost((String) config.get("WEBSERVER.URL"));
        thisSite.setLanguage((String) config.get("SYSTEM.LANGUAGE"));
        siteList.add(thisSite);
      }
    } catch (Exception e) {
      return null;
    } finally {
      if (cp != null) {
        cp.free(dbSites);
      }
    }
    return siteList;
  }


  public static SiteList getSiteList(MVCConfig prefs, ConnectionPool cp, ConnectionElement ce) {
    return SiteUtils.getSiteList(prefs.getPrefs(), cp, ce);
  }


  public static SiteList getSiteList(Map config, ConnectionPool cp, ConnectionElement ceToCompare) {
    Connection dbSites = null;
    SiteList siteList = new SiteList();
    String appCode = (String) config.get(MVCConstant.DB_CODE);
    String baseName = (String) config.get("gatekeeper.url");
    String dbUser = (String) config.get("gatekeeper.user");
    String dbPass = (String) config.get("gatekeeper.password");
    String driver = (String) config.get("gatekeeper.driver");
    try {
      if (MVCConfig.getConfig().isSiteMode()) {
        if (cp != null) {
          ConnectionElement ce = new ConnectionElement(
              baseName, dbUser, dbPass);
          ce.setDriver(driver);
          dbSites = cp.getConnection(ce);
        } else {
          //Build list of sites to process
          Class.forName(driver);
          dbSites = DatabaseUtils.getConnection(baseName, dbUser, dbPass);
        }
        siteList.setSiteCode(appCode);
        siteList.setDbHost(ceToCompare.getUrl());
        siteList.setEnabled(MVCConstant.TRUE);
        siteList.buildList(dbSites);
        if (cp == null) {
          dbSites.close();
        }
      } else {
        //This setup only allows one site so process it
        Site thisSite = new Site();
        thisSite.setDatabaseDriver((String) config.get("gatekeeper.driver"));
        thisSite.setDatabaseUrl((String) config.get("gatekeeper.url"));
        thisSite.setDatabaseName((String) config.get("gatekeeper.database"));
        thisSite.setDatabaseUsername((String) config.get("gatekeeper.user"));
        thisSite.setDatabasePassword(
            (String) config.get("gatekeeper.password"));
        thisSite.setSiteCode((String) config.get(MVCConstant.DB_CODE));
        thisSite.setVirtualHost((String) config.get("WEBSERVER.URL"));
        thisSite.setLanguage((String) config.get("SYSTEM.LANGUAGE"));
        siteList.add(thisSite);
      }
    } catch (Exception e) {
      return null;
    } finally {
      if (cp != null) {
        cp.free(dbSites);
      }
    }
    return siteList;
  }

}
